/**
 * AdMob.m
 * AdMob iPhone SDK publisher code.
 *
 * Sample code. See AdMob.h for instructions.
 */
#if LG_USE_ADMOB

#import "AdMobView.h"
#import "AdMob.h"
#import "LiquidGearAppDelegate.h"
#import "Utils.h"
#import "WebController.h"

@implementation AdMob

@synthesize viewController;

- (id)initWithController:(WebController*)controller {
	viewController	= controller;
	self.view.hidden = YES;
	adMobAd	= [AdMobView requestAdWithDelegate:self];
	[adMobAd retain];
	
	CGRect winRect	= [[UIScreen mainScreen] applicationFrame];
	self.view.frame = CGRectMake(0, 0, winRect.size.width, 48);
	
	return self;
}

- (void)awakeFromNib {
  // If using this object in a nib other than MainWindow.xib (like this example), keep the following [self retain] line, else this object will be autoreleased
  // out of existence -- see http://developer.apple.com/releasenotes/DeveloperTools/RN-InterfaceBuilder/index.html#//apple_ref/doc/uid/TP40001016-SW5
  // section "FAQ: What is different about NIB loading in a Cocoa Touch application?"
  // You can tell that this is occurring if you never get any calls back to -didReceiveAd: or -didFailToReceiveAd:.
  //[self retain];
  self.view.hidden = YES; // when there's no ad, let our (placeholder) view be unobstructive
  adMobAd = [AdMobView requestAdWithDelegate:self]; // start a new ad request
  [adMobAd retain]; // this will be released when it loads (or fails to load)
}

- (void)dealloc {
  [refreshTimer invalidate];
  [adMobAd release];
  [super dealloc];
}

// Request a new ad. If a new ad is successfully loaded, it will be animated into location.
- (void)refreshAd:(NSTimer *)timer {
  [adMobAd requestFreshAd];
}

- (void)hideBar {
	LiquidGearAppDelegate *LGdelegate = (LiquidGearAppDelegate*)[[UIApplication sharedApplication] delegate];
	
	if(LGdelegate.useAdMob) {
		self.view.hidden = YES;
		
		//Resize Frame
		CGRect winRect	= LGdelegate.currentRect;
		CGRect appRect	= [[UIScreen mainScreen] applicationFrame];
		
		if([LG_ADMOB_POSITION isEqualToString:@"top"]) {
			winRect.size.height	+= 48;
			
			if(winRect.origin.y > 0) {
				winRect.origin.y	-= 48;
			}
		} else {
			if(winRect.size.height < appRect.size.height) {
				winRect.size.height	+= 48;
			}
		}
		
		LGdelegate.useAdMob						= FALSE;
		LGdelegate.currentRect				= winRect;
		viewController.webView.frame	= winRect;
		
		//[LGdelegate.window bringSubviewToFront:viewController.webView];
	}
}

- (void)showBar:(NSString *)position {
	LiquidGearAppDelegate *LGdelegate = (LiquidGearAppDelegate*)[[UIApplication sharedApplication] delegate];
	
	if([position isEqualToString:@"top"]) {
		LG_ADMOB_POSITION	= @"top";
	} else {
		LG_ADMOB_POSITION	= @"bottom";
	}
	
	if(!LGdelegate.useAdMob) {
		self.view.hidden		= NO;
		LGdelegate.useAdMob	= TRUE;
		
		CGRect winRect	= viewController.webView.frame;
		CGRect adRect		= self.view.frame;
		
		winRect.size.height	-= 48;
		
		if([LG_ADMOB_POSITION isEqualToString:@"top"]) {
			adRect.origin.y		= winRect.origin.y;
			self.view.frame		= adRect;
			
			winRect.origin.y	+= 48;
		} else {
			adRect.origin.y		= winRect.size.height + winRect.origin.y;
			self.view.frame		= adRect;
		}
		
		viewController.webView.frame	= winRect;
	}
}

#pragma mark -
#pragma mark AdMobDelegate methods

- (NSString *)publisherId {
	// This should be set in the Settings.plist file;
  return LG_ADMOB_PUBID;
}

- (UIColor *)adBackgroundColor {
  UIColor *bgColor	= [[[Utils alloc] autorelease] txtToColor:LG_ADMOB_BGCOLOR];
	return bgColor;
}

- (UIColor *)primaryTextColor {
  UIColor *txtColor	= [[[Utils alloc] autorelease] txtToColor:LG_ADMOB_TXTCOLOR1];
	return txtColor;
}

- (UIColor *)secondaryTextColor {
  UIColor *txtColor	= [[[Utils alloc] autorelease] txtToColor:LG_ADMOB_TXTCOLOR2];
	return txtColor;
}

- (BOOL)mayAskForLocation {
  return LG_ADMOB_ASKLOCATION;
}

- (BOOL)useTestAd {
  return LG_ADMOB_TEST;
}

- (NSString *)testAdAction {
  return @"http://www.liquidgear.net"; // see AdMobDelegateProtocol.h for a listing of valid values here
}

// Sent when an ad request loaded an ad; this is a good opportunity to attach
// the ad view to the hierachy.
- (void)didReceiveAd:(AdMobView *)adView {
  NSLog(@"AdMob: Did receive ad");
  self.view.hidden = NO;
  adMobAd.frame = [self.view convertRect:self.view.frame fromView:self.view.superview]; // put the ad in the placeholder's location
  [self.view addSubview:adMobAd];
  [refreshTimer invalidate];
  refreshTimer = [NSTimer scheduledTimerWithTimeInterval:AD_REFRESH_PERIOD target:self selector:@selector(refreshAd:) userInfo:nil repeats:YES];
}

// Sent when an ad request failed to load an ad
- (void)didFailToReceiveAd:(AdMobView *)adView {
  NSLog(@"AdMob: Did fail to receive ad");
  [adMobAd release];
  adMobAd = nil;
	
	[self hideBar];
  // we could start a new ad request here, but it is unlikely that anything has changed in the last few seconds,
  // so in the interests of the user's battery life, let's not
}

@end
#endif